--[[
	游戏服调度机器人接口
--]]

local skynet = require "skynet"
local cluster = require "skynet.cluster"
local filelog = require "filelog"
local base = require "base"
local filename = "fishscheduler.lua"

local gamename = "fishnode"

local fishscheduler = {
	CMD = {},
    is_run = false,       --是否运行
}

local PLAYER_MAX = 10

---返回机器人的数量
function fishscheduler.CMD.getrobotnum()

end

function fishscheduler.CMD.change_schedule_state(newstate)
    -- fishscheduler.is_run = newstate
end

function fishscheduler.CMD.robotentertable(table_index, robotnum, robotconf)
	local status, result = skynet.pcall(skynet.call, ".schedulemgr", "lua", "request_robot_enter", gamename, table_index, robotnum, robotconf)
end

function fishscheduler.CMD.robotentermatchtable(match_type, match_level, robotnum, enter_score)
    --入场，底注随机3~5倍
    local min = 7500    -- 1 * enter_score 
    local max = 8500    -- 5 * enter_score
    local moneyvalue = base.get_random(min, max)
    local robotconf = {diamond = 0, coin = 0, logicname = "fish"}
    if match_type == "diamond" then
        robotconf.diamond = moneyvalue
    elseif match_type == "coin" then
        robotconf.coin = moneyvalue
    end
    skynet.call(".schedulemgr", "lua", "request_robot_match", gamename, match_type, match_level, robotnum, robotconf)
end

function fishscheduler.request_createtable()
    --创建自建桌 底注范围(50-100金豆) 人数条件(非满人桌不足2桌)
    local table_conf = {
		game_type = 1,
    }
    local rdata = {
        money = {diamond = 100000,},
        info = {rolename = '', logo = ''}
    }
    --建桌并进桌
    local status, result = skynet.pcall(cluster.call, gamename, ".router", "createtable", 101, rdata, {config = table_conf})
    if status and result and result.retcode == nil then
        fishscheduler.CMD.robotentertable(result.table_index, 1, {
            diamond = math.random(7500, 8500),
            logicname = "fish",
        })
    end
end

function fishscheduler.check_alltableinfo()
    --桌子数量
    -- local ptable_nums = 0
	local status, result = skynet.pcall(cluster.call, gamename, ".router", "get_alltableinfo")
    if status then
        -- --[[
        if result.pub_tableinfos and result.all_pubnum < PLAYER_MAX then    --只处理公开桌
            for table_index, tableinfo in pairs(result.pub_tableinfos) do
                if tableinfo.cur_player > 0 and tableinfo.cur_player < 2 and tableinfo.robot_num == 0 then   --小于2人，加入机器人
                    fishscheduler.CMD.robotentertable(table_index, 1, {
                        diamond = math.random(7500, 8500),
                        logicname = "fish",
                    })
                end
                -- ptable_nums = ptable_nums + 1
            end
        end
    end

    ----------------
    ---建桌调度

    --保证1个50底注桌，1个100底注桌
    -- if ptable_nums == 0 then
    --     fishscheduler.request_createtable()
    -- end
end

function fishscheduler.monitor()
	-- body
	skynet.fork(function()
        while true do
        	skynet.sleep(6 * 100)
            ----定时拉取游戏所有桌子信息
            if fishscheduler.is_run then
                pcall(fishscheduler.check_alltableinfo)
            end
        end
    end)
end

skynet.start(function()
	skynet.dispatch("lua",function(session, source, cmd, ...)
        local f = fishscheduler.CMD[cmd]
        if f then
            skynet.retpack(f(...))
        else
            skynet.retpack()
        end
    end)
    fishscheduler.monitor()
end)
